package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.BuyHMioAddData;
import com.alm.token.ui.data.bean.LH2OrderDetailsData;
import com.alm.token.ui.data.bean.QuantitativeAShouyiListData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.QuantitativeOrder2View;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QuantitativeShouyiListActivity extends BaseActivity implements QuantitativeOrder2View<QuantitativeAShouyiListData> {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;

    private RefreshLoadMoreManager<QuantitativeAShouyiListData.DataBean.WealthMapDataBean.WealthListDataBean> manager;
    private List<QuantitativeAShouyiListData.DataBean.WealthMapDataBean.WealthListDataBean> dataBeans = new ArrayList<>();
    private List<QuantitativeAShouyiListData.DataBean.WealthMapDataBean.WealthListDataBean> dataBeansOld = new ArrayList<>();

    private List<QuantitativeAShouyiListData.DataBean.WealthMapDataBean> dataBeans1 = new ArrayList<>();
    private List<QuantitativeAShouyiListData.DataBean.WealthMapDataBean> dataBeansOld1 = new ArrayList<>();

    private int limit = 100, page = 1;
    private boolean isfull = false;

    public QuantitativeShouyiListActivity() {
    }

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_lh2orderhistory;
    }

    @Override
    protected void initVariables() {

    }


    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.shouyi_hint_detail));
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
        nodatashowBody.setVisibility(View.VISIBLE);
    }

    private void initManager() {
        OrderAdapter adapter = new OrderAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
//                        if (dataBeansGuanbi != null && dataBeansGuanbi.size() > position) {
//                            Intent intent = new Intent(LH2OrderHistoryActivity.this, QuantitativeOrderDetailActivity.class);
//                            intent.putExtra("databean", dataBeansGuanbi.get(position));
//                            startActivityForResult(intent, ActivityForResultCode);
//                        }
//                        startActivity(QuantitativeOrderDetailActivity.class);
                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadMoreData();
                    }
                })
                .setOpenLoadMore(true)
                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {
                        manager.getAdapter().loadMoreEnd(true);
                        manager.getAdapter().loadMoreFail();
                        showWaitDialog();
                        preenter.getLH2OrderCaifuListData(String.valueOf(limit), String.valueOf(page));
                    }
                })
                .build();

        recyclerView.setNestedScrollingEnabled(false);

    }

    @Override
    protected void loadData() {
        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
        preenter.getLH2OrderCaifuListData(String.valueOf(limit), String.valueOf(page));
    }

    void loadMoreData() {
        limit = 100;
        page = 1;
        preenter.getLH2OrderCaifuListData(String.valueOf(limit), String.valueOf(page));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.rightItem:
//                startActivity(LH2OrderHistoryActivity.class);
//                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void getData(QuantitativeAShouyiListData quantitativeAShouyiListData) {

        if (quantitativeAShouyiListData.getStatus() == 0) {
            if (page == 1) {
                dataBeans1.clear();
                dataBeans.clear();
            }else if(!isfull&&dataBeans1.size()>(page-1)*100){
                dataBeans1.removeAll(dataBeansOld1);
                dataBeans.removeAll(dataBeansOld);
            }

            if(quantitativeAShouyiListData.getData().getWealthMapData().size()==100){
                isfull=true;
                page=page+1;
            }else {
                isfull=false;
            }
            dataBeansOld1 = quantitativeAShouyiListData.getData().getWealthMapData();
            dataBeans1.addAll(quantitativeAShouyiListData.getData().getWealthMapData());

            dataBeansOld.clear();
            for (QuantitativeAShouyiListData.DataBean.WealthMapDataBean wealthMapDataBean:dataBeans1) {
                for (int i = 0; i <wealthMapDataBean.getWealthListData().size() ; i++) {
                    if(i==0){
                        wealthMapDataBean.getWealthListData().get(i).setDate(wealthMapDataBean.getName());
                    }
                    wealthMapDataBean.getWealthListData().get(i).setLayoutId(OrderAdapter.LAYOUT_ID);
                    dataBeansOld.add(wealthMapDataBean.getWealthListData().get(i));
                }
            }
            dataBeans.addAll(dataBeansOld);
            manager.setData(dataBeans);
        } else {
            showToast(quantitativeAShouyiListData.getMsg());
        }
        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @Override
    public void getLH2OrderOrderDetails(LH2OrderDetailsData lh2OrderDetailsData) {

    }

    @Override
    public void getLH2OrderOrderDetails(BuyHMioAddData buyHMioAddData) {

    }

    public class OrderAdapter extends BaseAdapter<QuantitativeAShouyiListData.DataBean.WealthMapDataBean.WealthListDataBean> {

        public static final int LAYOUT_ID = R.layout.item_quantitative_order_shouyi;

        public OrderAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, QuantitativeAShouyiListData.DataBean.WealthMapDataBean.WealthListDataBean item, int itemType) {

            helper.setGone(R.id.tv_time,EmptyUtils.isEmpty(item.getDate())?false:true);
            helper.setText(R.id.tv_time, item.getDate());
            helper.setText(R.id.tv_shouyitime, item.getName());
            helper.setTextColor(R.id.tv_shouyitime, context.getResources().getColor(R.color.text_2223292));
            helper.setText(R.id.tv_shouymoney, item.getMoney());
            helper.setTextColor(R.id.tv_shouymoney, context.getResources().getColor(R.color.bg_653fac2));
        }

    }

}
